package com.zkca.xf.ui.activity;

import android.content.Intent;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushManager;
import com.videogo.openapi.EZOpenSDK;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseActivity;
import com.zkca.xf.model.javabean.xg.CustomContentBean;
import com.zkca.xf.ui.activity.news.NewsActivity;
import com.zkca.xf.ui.activity.personprotect.FaultJob_LActivity;
import com.zkca.xf.ui.activity.personprotect.Job_LActivity;
import com.zkca.xf.ui.activity.personprotect.MesActivity;
import com.zkca.xf.ui.activity.technicalprotect.Dianhu_LActivity;
import com.zkca.xf.ui.activity.technicalprotect.Dianhu_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.ElectriG_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.ElectriZ_X_Activity;
import com.zkca.xf.ui.activity.technicalprotect.ElectricityF_LActivity;
import com.zkca.xf.ui.activity.technicalprotect.ElectricityF_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.FireAlarm_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.FireControl_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.JingGaiNB_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.SmokeNB_LActivity;
import com.zkca.xf.ui.activity.technicalprotect.SmokeNB_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.Smoke_LActivity;
import com.zkca.xf.ui.activity.technicalprotect.Smoke_XActivity;
import com.zkca.xf.ui.activity.technicalprotect.Water_XActivity;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.StringUtils;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


/**
 * Created by pc on 2018/8/3.
 */

public class SplashActivity extends BaseActivity {
    private LinearLayout ll_bg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }

        setContentView(R.layout.activity_splash);
        initview();
        getyingshiToken();


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);// 必须要调用这句
    }

    @Override
    protected void onPause() {
        super.onPause();
        XGPushManager.onActivityStoped(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        XGPushClickedResult click = XGPushManager.onActivityStarted(this);
//        click.getCustomContent();
        Log.d("TPush", "onResumeXGPushClickedResult:" + click);
        if (click != null) { // 判断是否来自信鸽的打开方式

            LogUtil.e("1212", click.toString());
            String customContent = click.getCustomContent();
            if (customContent != null && customContent.length() != 0) {
                Gson gson = new Gson();
                CustomContentBean customContentBean = gson.fromJson(customContent, CustomContentBean.class);
                String module = customContentBean.getModule();
                int function = customContentBean.getFunction();
                int record = customContentBean.getRecord();
                Intent[] intents;
                Intent mainIntent = new Intent(this, MainActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                if (module.equals("0001")) {

                    Intent intent = new Intent(this, FireAlarm_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>FireAlarm_XActivity");

                } else if (module.equals("0002")) {
                    Intent smke_l = new Intent(this, Smoke_XActivity.class);
                    smke_l.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, smke_l};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>Smoke_LActivity");
                } else if (module.equals("0003")) {

                    Intent intent = new Intent(this, Dianhu_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>Dianhu_XActivity");

                } else if (module.equals("0004")) {

                    Intent intent = new Intent(this, Water_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>Water_XActivity");

                } else if (module.equals("0005")) {
                    Intent el_fintent = new Intent(this, FireControl_XActivity.class);
                    el_fintent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, el_fintent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>FireControl_XActivity");
                } else if (module.equals("0006")) {
//                    Intent intent=new Intent(this, Water_XActivity.class);
//                    intent.putExtra("XG",record);
//                    intents = new Intent[]{mainIntent, intent};
//                    this.startActivities(intents);
//                    LogUtil.e("1212","=======>Water_XActivity");

                } else if (module.equals("0007")) {

                    Intent intent = new Intent(this, ElectriZ_X_Activity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>ElectriZ_X_Activity");

                } else if (module.equals("0008")) {

                    Intent intent = new Intent(this, ElectricityF_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>Water_XActivity");

                } else if (module.equals("0009")) {
                    if (function == 2) {
                        Intent intent = new Intent(this, Job_LActivity.class);
                        intent.putExtra("taskId", record);
                        intents = new Intent[]{mainIntent, intent};
                        this.startActivities(intents);
                        LogUtil.e("1212", "=======>Job_LActivity");
                    } else if (function == 1) {
                        Intent intent = new Intent(this, MesActivity.class);
                        intent.putExtra("taskId", record);
                        intents = new Intent[]{mainIntent, intent};
                        this.startActivities(intents);
                        LogUtil.e("1212", "=======>MesActivity");
                    } else if (function ==3) {
                        Intent intent = new Intent(this, FaultJob_LActivity.class);
                        intents = new Intent[]{mainIntent, intent};
                        this.startActivities(intents);
                        LogUtil.e("1212", "=======>FaultJob_LActivity");
                    }
                } else if (module.equals("0010")) {
                    Intent intent = new Intent(this, ElectriG_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>ElectriG_XActivity");
                }else if (module.equals("0011")) {
                    Intent intent = new Intent(this, SmokeNB_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>ElectriG_XActivity");
                } else if (module.equals("0012")) {
                    Intent intent = new Intent(this, JingGaiNB_XActivity.class);
                    intent.putExtra("XG", record);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("1212", "=======>ElectriG_XActivity");
                } else if (module.equals("0000")) {
                    Intent intent = new Intent(this, NewsActivity.class);
                    //intent.putExtra("XG", record);
                    String url = customContentBean.getExtend().getUrl();
                    intent.putExtra("URL", url);
                    intents = new Intent[]{mainIntent, intent};
                    this.startActivities(intents);
                    LogUtil.e("111111111111", "====1324===>NewsActivity");
                    this.startActivities(intents);
                } else {
                    this.startActivity(new Intent(this, MainActivity.class));
                }
            } else {
                this.startActivity(new Intent(this, MainActivity.class));
            }
        } else {
            initData();
        }
    }

    private void getyingshiToken() {
        Map map = new HashMap();
        map.put("appKey", "059ceee36b2c4a8fb56587a860f2ad8b");
        map.put("appSecret", "32c6602a3d98bfbc8529222db0d09293");
        OkHttpUtils.post().url("https://open.ys7.com/api/lapp/token/get").params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                // ToastUtils.show(SplashActivity.this,"萤石Token获取失败"
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    LogUtil.e("token==", response);
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data = (JSONObject) jsonObject.get("data");
                    String accessToken = (String) data.get("accessToken");
                    long expireTime = (long) data.get("expireTime");
                    String formatDateYMD = TimeUtils.getFormatDateYMD(expireTime);
                    LogUtil.e("过期时间", formatDateYMD);
                    SPUtil.putString(SplashActivity.this, Constants_SP.TOKEN_YINGSHI, accessToken);
                    //    ToastUtils.show(SplashActivity.this,"萤石Token获取成功");
                    EZOpenSDK.getInstance().setAccessToken(accessToken);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void initview() {
        ll_bg = (LinearLayout) findViewById(R.id.ll_bg);

    }

    private void initData() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                int depId = SPUtil.getInt(SplashActivity.this, Constants_SP.DEPID, -199);
                String server_address = SPUtil.getString(SplashActivity.this, Constants_SP.SERVER_ADDRESS, "");

                LogUtil.e("服务器地址", server_address);

                if (depId != -199) {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    // SplashActivity.this.finish();
                } else if (depId == -199 && !TextUtils.isEmpty(server_address)) {
                    Intent intent = new Intent(SplashActivity.this, Activity_Login_Code.class);
                    startActivity(intent);
                    //SplashActivity.this.finish();
                } else {
                    Intent intent = new Intent(SplashActivity.this, IpSettingActivity.class);
                    startActivity(intent);
                    // SplashActivity.this.finish();
                }
            }
        }, 2000);
    }

}
